home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP9_91.ARJ / KBFLIP.C < prev    next >
C/C++ Source or Header  |  1991-08-17  |  3KB  |  90 lines

  1. /*
  2. **  KBFLIP.C
  3. **
  4. **  a public domain demo by: Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <process.h>
  11.  
  12. #ifdef __TURBOC__
  13.  #define FAR far
  14. #else
  15.  #define FAR _far
  16. #endif
  17.  
  18. #define SHOW(str) fputs(str"\n", stderr)
  19.  
  20. #define BitSet(arg,posn) ((arg) | (1L << (posn)))
  21. #define BitClr(arg,posn) ((arg) & ~(1L << (posn)))
  22.  
  23. #define LOCKS_POSN 4
  24.  
  25. unsigned char FAR *kb_status = (unsigned char FAR *) 0x00400017;
  26.  
  27. /*
  28. **  Tell the folks how this works
  29. **
  30. **  Note that this function calls _exit() and never returns to the caller.
  31. **  The reason _exit() is called rather than exit() is to minimize the
  32. **  size of the library code linked in.
  33. */
  34.  
  35. void usage(void)
  36. {
  37.       SHOW("Usage: KBFLIP {+|-}[switches] [...{+|-}[switches]]");
  38.       SHOW("Where \"switches\" are one or more of:");
  39.       SHOW(" +/-C - Turn Caps Lock on/off");
  40.       SHOW(" +/-N - Turn Num Lock on/off");
  41.       SHOW(" +/-S - Turn Scroll Lock on/off");
  42.       SHOW("Note switches may be upper or lower case\n");
  43.       SHOW("Example: \"KBFLIP +Cn -S\" turns Caps Lock and Num Lock on "
  44.             "and Scroll lock off");
  45.       _exit(-1);
  46. }
  47.  
  48. /*
  49. **  The real works starts here
  50. **
  51. **  This works by checking the user input against a string containing the
  52. **  allowable switch characters in the same relative positions they
  53. **  occupy in the BIOS data area, offset by 4 (LOCKS_POSN).
  54. **
  55. **  Note that all changes are made to a copy of the BIOS data so any
  56. **  input errors will not cause incomplete changes to be applied.
  57. */
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.       int i, j;
  62.       char *args = "SNC";
  63.       unsigned char template = *kb_status;      /* Make changes to copy */
  64.  
  65.       if (2 > argc)                             /* Help 'em             */
  66.             usage();
  67.       for (i = 1; i < argc; ++i)
  68.       {
  69.             if (NULL == strchr("+-", *argv[i]))
  70.                   usage();
  71.  
  72.             for (j = 1; argv[i][j]; ++j)
  73.             {
  74.                   char *found;
  75.  
  76.                   if (NULL != (found = strchr(args, toupper(argv[i][j]))))
  77.                   {
  78.                         int posn = LOCKS_POSN + (found - args);
  79.  
  80.                         if ('+' == *argv[i])
  81.                               template = BitSet(template, posn);
  82.                         else  template = BitClr(template, posn);
  83.                   }
  84.                   else  usage();
  85.             }
  86.       }
  87.       *kb_status = template;                    /* Apply all changes    */
  88.       _exit(0);
  89. }
  90.